import { get, post } from './request'

/**
 * 公告查询参数
 */
export interface AnnouncementQueryParams {
  pageNum?: number
  pageSize?: number
  title?: string
  type?: string
  status?: number
  priority?: number
  publisherName?: string
  startTime?: string
  endTime?: string
}

/**
 * 公告信息
 */
export interface AnnouncementInfo {
  id: number
  title: string
  content?: string
  type?: string
  priority?: number
  status?: number
  publishTime?: string
  expireTime?: string
  publisherId?: number
  publisherName?: string
  targetType?: string
  targetIds?: string
  isTop?: number
  topExpireTime?: string
  viewCount?: number
  attachmentIds?: string
  createdAt?: string
  updatedAt?: string
  isNew?: boolean // 前端计算的标识
}

/**
 * 分页结果
 */
export interface PageResult<T> {
  records: T[]
  total: number
  current: number
  size: number
  pages: number
}

/**
 * 获取公告列表（仅获取已发布的公告）
 * @param params 查询参数
 * @returns Promise 返回公告列表
 */
export function getAnnouncementList(params?: AnnouncementQueryParams) {
  return post<PageResult<AnnouncementInfo>>('/announcement/page', {
    pageNum: params?.pageNum || 1,
    pageSize: params?.pageSize || 10,
    status: 1, // 只查询已发布的公告
    ...params,
  })
}

/**
 * 根据ID获取公告详情
 * @param id 公告ID
 * @returns Promise 返回公告详情
 */
export function getAnnouncementDetail(id: number) {
  return get<AnnouncementInfo>(`/announcement/${id}`)
}

/**
 * 获取首页公告列表（最新3条）
 * @returns Promise 返回公告列表
 */
export function getHomeAnnouncementList() {
  return post<PageResult<AnnouncementInfo>>('/announcement/page', {
    pageNum: 1,
    pageSize: 3,
    status: 1, // 只查询已发布的公告
  })
}
